package br.ufc.parser;

public class Value {
	private Object value;
	public Value(String value){
		try{
			this.value = Double.parseDouble(value.replaceAll("\'", ""));
		}catch(Exception e){
			this.value = value.replaceAll("\'", "");
		}		
	}
	public Value(Double value){
		this.value = value;
	}
	public boolean isDouble(){
		return value instanceof Double;
	}
	public boolean isString(){
		return value instanceof String;
	}
	public String getStringValue(){
		if(this.isString()){
			return (String)this.value;
		}
		return null;
	}
	public Double getDoubleValue(){
		if(this.isDouble()){
			return (Double)this.value;
		}
		return null;
	}

	public String toString(){
		if(this.isString()){
			return (this.getStringValue());
		}else{
			return ((Double)this.getDoubleValue()).toString();
		}
	}

	public int compareTo(Value other){
		if(this.isString() && other.isString()){
			String stringObj1 = this.getStringValue();
			String stringObj2 = other.getStringValue();
			return stringObj1.compareToIgnoreCase(stringObj2);
		}else if(this.isDouble() && other.isDouble()){
			Double doubleObj1 = this.getDoubleValue();
			Double doubleObj2 = other.getDoubleValue();
			return doubleObj1.compareTo(doubleObj2);
		}
		throw new RuntimeException("Should not happen");
	}
	@Override
	public int hashCode() {
		int result;
		if(isDouble()){
			final int prime = 31;
			result = 1;
			int hash_value = getDoubleValue().intValue();
			result = prime * result + hash_value;
		}else{
			final int prime = 31;
			result = 1;
			result = prime * result + ((value == null) ? 0 : value.hashCode());			
		}
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Value other = (Value) obj;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}
}
